//
//  StatusesCell.swift
//  Microblog
//
//  Created by steaest on 15/8/5.
//  Copyright © 2015年 steaest. All rights reserved.
//

import UIKit

//控件间距
let StatusesCellMargin: CGFloat = 8

//微博Cell的标示符枚举
enum StatusCellIdentifier: String {

    case NormalCell = "NormalCell"
    case forwardCell = "forwardCell"
    
    //静态函数
    //根据微博数据,返回对应的cell
    static func cellId(status:Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.forwardCell.rawValue
    }
}

class StatusesCell: UITableViewCell {

    var statuses: Status? {
        didSet {
        
            topView.status = statuses
            contentLabel.text = statuses?.text ?? ""
            
            pictureView.status = statuses
            // 设置配图视图的尺寸
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureWidthCons?.constant = pictureView.bounds.size.width
            
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : StatusesCellMargin
        }
    }
    
    //MARK: - 计算行高
    func rowHeight(status: Status) -> CGFloat {
        //设置属性
        self.statuses = status
        //强行更新布局 - 所有控件的frame都会发生变化
        //使用自动布局,不要直接修改frame,修改的工作交给系统
        layoutIfNeeded()
        //TODO: - (BUHUI)返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //图片的宽高约束
    var pictureWidthCons: NSLayoutConstraint?
    var pictureHeightCons: NSLayoutConstraint?
    //图片的顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    //MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 界面设置
    func setupUI() {
    
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //自动布局(顶部视图)
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSizeMake(UIScreen.mainScreen().bounds.width, 53))
        
        //内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: StatusesCellMargin, y: StatusesCellMargin))
        //固定宽度(为什么没有设false属性)
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * StatusesCellMargin))
        
        //底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 44), offset: CGPoint(x: -StatusesCellMargin, y: StatusesCellMargin))
        //底部约束
        //contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    
    //MARK: - 懒加载控件
    //顶部视图
    private lazy var topView: StatusesTopView = StatusesTopView()
    //内容
    lazy var contentLabel: UILabel = {
        let l = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        l.numberOfLines = 0
        //设置文本折行宽度,如果不设置有时候行高会计算的不准确.(例如小白条)
        l.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 2 * StatusesCellMargin
        return l
    }()
    //底部视图
    lazy var bottomView: StatusesBottomView = StatusesBottomView()
    //图片
    lazy var pictureView: StatusPicture = StatusPicture()
}
